//
//  HomeTabBarController.m
//  suRead
//
//  Created by lidi on 16/4/21.
//  Copyright © 2016年 bao. All rights reserved.
//

#import "HomeTabBarController.h"
#import "CustomTabBarView.h"

#import "NewsRootViewController.h"
#import "PocketNewspapersViewController.h"
#import "ServingViewController.h"
#import "ChutianPlusViewController.h"
#import "QRViewController.h"
#import "ReportListViewController.h"
#import "CustomerServiceViewController.h"
#import "LeftMenuViewController.h"
#import "NewPersonalCenterViewController.h"

#import "UIApplication+VisibleViewController.h"
#import "NewsWebViewController.h"
#import "NewsSpeechManager.h"
#import "ServiceUserNew.h"

@interface HomeTabBarController ()
@property(nonatomic,strong)CustomTabBarView *customTabBar;
@end

@implementation HomeTabBarController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
}

-(void)applicationWillEnterForeground {
    NSLog(@"xxxxxxxxx7777777777777777xxx");
}

//-(void)viewDidDisappear:(BOOL)animated {
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 登录成功，增加积分,打开app执行一次
    [ServiceUserNew doAddScore:AddScoreType_Login
                       success:^(id  _Nonnull responseObject, NSString * _Nonnull message) {}
                       failure:^(NSString * _Nonnull message) {}];
    
    // 修改user-agent
    NSString *agentStrToAdd = @",KCT/8";
    UIWebView *webView = [UIWebView new];
    NSString *oldAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
    NSLog(@"old agent :%@", oldAgent);
    if ([oldAgent rangeOfString:agentStrToAdd].location == NSNotFound) {
        NSString *newAgent = [oldAgent stringByAppendingString:agentStrToAdd];
        NSLog(@"new agent :%@", newAgent);
        NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:newAgent, @"UserAgent", nil];
        [[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
    }

    // 界面配置
    self.viewControllers = @[ [NewsRootViewController new],
                              [ChutianPlusViewController new],
                              [CustomerServiceViewController new],
                              [PocketNewspapersViewController new],
                              [NewPersonalCenterViewController new] ];
    
    __weak typeof(self) weak_self = self;
    //终极解决办法
    self.customTabBar = [CustomTabBarView customTabBarViewWithSelectAtIndexBlock:^(NSUInteger index) {
        weak_self.selectedIndex = index;
    }];
    
    [self.customTabBar setGridServiceBlock:^{
        //QRViewController *controller = [QRViewController new];
        //LeftMenuViewController *controller = [[LeftMenuViewController alloc] initWithNibName:@"LeftMenuViewController" bundle:[NSBundle mainBundle]];
        NewPersonalCenterViewController *controller = [NewPersonalCenterViewController new];
        controller.hidesBottomBarWhenPushed = YES;
        [[[UIApplication sharedApplication] visibleViewController] showViewController:controller sender:nil];
    }];
    
    self.tabBar.backgroundImage = [[weak_self class] createImageWithColor:[UIColor whiteColor]];
    self.tabBar.shadowImage = [[weak_self class] createImageWithColor:[UIColor clearColor]];
    self.customTabBar.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 49.0f);
    [self.tabBar addSubview:self.customTabBar];
}

+ (UIImage *)createImageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

- (void)touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event{
    NSLog(@"fff");
}

@end
